import { defineStore } from "pinia";
import { ref } from "vue";
import { identityApi, contactApi } from "../api";

export const useUserStore = defineStore("user", () => {
  // 状态
  const identities = ref([]);
  const contacts = ref([]);
  const loading = ref(false);

  // 方法
  const fetchIdentities = async () => {
    try {
      loading.value = true;
      const response = await identityApi.getIdentities();
      identities.value = response.data;
    } catch (error) {
      console.error("获取身份信息失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const addIdentity = async (identity) => {
    try {
      loading.value = true;
      const response = await identityApi.addIdentity(identity);
      identities.value.push(response.data);
    } catch (error) {
      console.error("添加身份信息失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const updateIdentity = async (id, identity) => {
    try {
      loading.value = true;
      const response = await identityApi.updateIdentity(id, identity);
      const index = identities.value.findIndex((item) => item.id === id);
      if (index > -1) {
        identities.value[index] = response.data;
      }
    } catch (error) {
      console.error("更新身份信息失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const deleteIdentity = async (id) => {
    try {
      loading.value = true;
      await identityApi.deleteIdentity(id);
      const index = identities.value.findIndex((item) => item.id === id);
      if (index > -1) {
        identities.value.splice(index, 1);
      }
    } catch (error) {
      console.error("删除身份信息失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const importIdentities = async (file) => {
    try {
      loading.value = true;
      const formData = new FormData();
      formData.append("file", file);
      const response = await identityApi.importIdentities(formData);
      identities.value = [...identities.value, ...response.data];
    } catch (error) {
      console.error("导入身份信息失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const fetchContacts = async () => {
    try {
      loading.value = true;
      const response = await contactApi.getContacts();
      contacts.value = response.data;
    } catch (error) {
      console.error("获取联系方式失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const addContact = async (contact) => {
    try {
      loading.value = true;
      const response = await contactApi.addContact(contact);
      contacts.value.push(response.data);
    } catch (error) {
      console.error("添加联系方式失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const updateContact = async (id, contact) => {
    try {
      loading.value = true;
      const response = await contactApi.updateContact(id, contact);
      const index = contacts.value.findIndex((item) => item.id === id);
      if (index > -1) {
        contacts.value[index] = response.data;
      }
    } catch (error) {
      console.error("更新联系方式失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const deleteContact = async (id) => {
    try {
      loading.value = true;
      await contactApi.deleteContact(id);
      const index = contacts.value.findIndex((item) => item.id === id);
      if (index > -1) {
        contacts.value.splice(index, 1);
      }
    } catch (error) {
      console.error("删除联系方式失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  const importContacts = async (file) => {
    try {
      loading.value = true;
      const formData = new FormData();
      formData.append("file", file);
      const response = await contactApi.importContacts(formData);
      contacts.value = [...contacts.value, ...response.data];
    } catch (error) {
      console.error("导入联系方式失败:", error);
      throw error;
    } finally {
      loading.value = false;
    }
  };

  return {
    // 状态
    identities,
    contacts,
    loading,

    // 方法
    fetchIdentities,
    addIdentity,
    updateIdentity,
    deleteIdentity,
    importIdentities,

    fetchContacts,
    addContact,
    updateContact,
    deleteContact,
    importContacts,
  };
});
